﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using AvalonDock;
using OpenExpressApp.Module.WPF;
using OpenExpressApp.Types;
using OpenExpressApp.Template;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Library;

namespace OpenExpressApp.WPF.Templete
{
    /// <summary>
    /// 从UserControl适配到IWindowTemplate的基类
    /// </summary>
    public abstract class WindowTemplateBase : UserControl, IWindowTemplate
    {
        private ListObjectView _listView;

        /// <summary>
        /// 当前显示的模型。（根对象。）
        /// </summary>
        private Type _rootEntityType;

        public WindowTemplateBase(Type rootEntityType)
        {
            Debug.Assert(rootEntityType != null, "没有提供对象模型！");

            this._listView = new ListObjectView(rootEntityType);

            this.Loaded += new RoutedEventHandler(WindowTemplateBase_Loaded);

            this._rootEntityType = rootEntityType;

            Zoom.EnableZoom(this);
        }

        /// <summary>
        /// 直接取根对象的名字作为Title
        /// </summary>
        public string Title
        {
            get
            {
                return ApplicationModel.GetBusinessObjectInfo(this._rootEntityType).Label;
            }
        }

        /// <summary>
        /// 当前显示的模型。（根对象。）
        /// </summary>
        protected Type RootEntityType
        {
            get
            {
                return this._rootEntityType;
            }
        }

        /// <summary>
        /// 只会发生一次Loaded事件。
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLoaded(RoutedEventArgs e)
        {
            //可能Net Framework的bug，Load会执行两遍，所以在这里去除监听。
            this.Loaded -= this.WindowTemplateBase_Loaded;

            this.LayoutQueryPanel();
        }

        private void WindowTemplateBase_Loaded(object sender, RoutedEventArgs e)
        {
            this.OnLoaded(e);
        }

        /// <summary>
        /// 根据listView的数据，布局查询面板和导航面板。
        /// 
        /// 注意：此方法与queryPane、conditionQueryRegion、navigateQueryRegion的结构耦合！
        /// </summary>
        /// <param name="listView"></param>
        /// <param name="queryPane"></param>
        /// <param name="conditionQueryRegion"></param>
        /// <param name="navigateQueryRegion"></param>
        public static void LayoutQueryPanel(ListObjectView listView, DockablePane queryPane, DockableContent dcCondition, DockableContent dcNavigate)
        {
            var hideNavigate = listView.NavigateQueryView == null ||
                ApplicationModel.GetBusinessObjectInfo(listView.BOType).NavigateQueryRegionPoistion == QueryRegionPosition.ResultView;
            var hideCondition = listView.CondtionQueryView == null;

            if (hideCondition)
            {
                queryPane.Items.Remove(dcCondition);

                if (hideNavigate == false)
                {
                    queryPane.SelectedIndex = 1;
                }
            }

            if (hideNavigate)
            {
                queryPane.Items.Remove(dcNavigate);

                if (hideCondition == false)
                {
                    queryPane.SelectedIndex = 0;
                }
            }

            if (hideCondition && hideNavigate)
            {
                (queryPane.Parent as Panel).Children.Remove(queryPane);
            }
        }

        #region IFrameTemplate Members

        public abstract ModuleType ModuleType { get; }

        public ListObjectView ListView
        {
            get
            {
                return this._listView;
            }
        }

        public ObjectView View
        {
            get
            {
                return this._listView;
            }
        }

        public abstract IFrameController Controller { get; }

        public abstract object GetRegion(string regionName);

        public abstract void LayoutQueryPanel();

        public object GetConditionQueryObject()
        {
            var view = this._listView.CondtionQueryView;

            if (null == view) return null;

            return view.CurrentObject;
        }

        public object GetNavigateQueryObject()
        {
            var view = this._listView.NavigateQueryView;

            if (null == view) return null;

            return view.CurrentObject;
        }

        #endregion

        public void GetObject(IQueryObject criteria)
        {
            this._listView.DataLoader.AsyncGetObject(criteria);
        }

        /// <summary>
        /// 当前的ListView是否有条件查询面板 或 导航查询面板。
        /// </summary>
        /// <returns></returns>
        protected bool HasQueryPanel()
        {
            return this._listView.CondtionQueryView != null || this._listView.NavigateQueryView != null;
        }
    }
}
